package com.yyw.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yyw.Service.TractateService;
import com.yyw.bean.Criticism;
import com.yyw.bean.Tractate;
import com.yyw.bean.User;
import com.yyw.dao.TractateDao;
import com.yyw.util.MyContext;
import com.yyw.util.Result;
import com.yyw.util.ResultFactory;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "短文API")
@RestController
@RequestMapping("/user")
@Slf4j
public class TractateController {

    @Value("${my.session.user}")
    private String userAttr;

    @Autowired
    private TractateService tractateService;

    @Autowired
    private TractateDao tractateDao;

    // 修改短文
    @CrossOrigin
    @ApiOperation(value = "修改短文", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "短文id", required = true),
            @ApiImplicitParam(name = "title", value = "短文标题", required = true),
            @ApiImplicitParam(name = "ftitle", value = "短文副标题", required = true),
            @ApiImplicitParam(name = "level", value = "短文难度", required = true),
            @ApiImplicitParam(name = "content", value = "短文内容", required = true) })
    @PostMapping("/updateTractate")
    public Result updateTractate(@RequestParam("id") Long id, @RequestParam("title") String title,
            @RequestParam("ftitle") String ftitle, @RequestParam("level") Integer level,
            @RequestParam("content") String content) {

        // 数据校验
        Tractate tractate = new Tractate(id, title, ftitle, level, content, null, null, false);
        if (id == null) {
            return ResultFactory.notice("id为空");
        }
        if (StringUtils.isEmpty(tractate.getTitle())) {
            return ResultFactory.notice("标题为空");
        }
        if (StringUtils.isEmpty(tractate.getFtitle())) {
            return ResultFactory.notice("副标题为空");
        }
        if (!tractate.checkLevel()) {
            return ResultFactory.notice("难度不合法");
        }
        if (StringUtils.isEmpty(tractate.getContent())) {
            return ResultFactory.notice("内容为空");
        }

        int num = tractateDao.updateTractate(tractate);
        if (num > 0) {
            return ResultFactory.success("修改成功");
        } else {
            return ResultFactory.failure("修改失败");
        }
    }

    // 删除短文
    @CrossOrigin
    @ApiOperation(value = "删除短文", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "短文id", required = true) })
    @PostMapping("/deleteTractate")
    public Result deleteTractate(@RequestParam("id") Long id) {
        if (id == null) {
            return ResultFactory.notice("数据部合法");
        }
        int i = tractateDao.deleteTractate(id);
        if (i > 0) {
            return ResultFactory.success("删除成功");
        } else {
            return ResultFactory.failure("删除失败");
        }
    }

    @CrossOrigin
    @ApiOperation(value = "根据评论id为评论点赞", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "评论id", required = true) })
    @GetMapping("/praiseCriticism")
    public Result praiseCriticism(@RequestParam("id") Long id) {

        return tractateService.praiseCriticism(id);
    }

    @CrossOrigin
    @ApiOperation(value = "根据短文id获取评论", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "tractateid", value = "短文id", required = true),
            @ApiImplicitParam(name = "pageNo", value = "当前页数", required = false),
            @ApiImplicitParam(name = "pageNum", value = "每页数量", required = false) })
    @GetMapping("/getCriticismsByTractateId")
    public Result getCriticismsByTractateId(@RequestParam("tractateid") Long tractateid,
            @RequestParam(value = "pageNo", defaultValue = "1", required = false) Integer pageNo,
            @RequestParam(value = "pageNum", defaultValue = "10", required = false) Integer pageNum) {
        return tractateService.getCriticismsByTractateId(tractateid, pageNo, pageNum);
    }

    @CrossOrigin
    @ApiOperation(value = "根据id获取短文", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "短文id", required = true),
            @ApiImplicitParam(name = "isCollect", value = "是否获取收藏状态，默认false", required = false) })
    @GetMapping("/getTractateById")
    public Result getTractateById(@RequestParam("id") Long id,
            @RequestParam(value = "isCollect", defaultValue = "false", required = false) Boolean isCollect) {
        Long userid = null;
        if (isCollect) {
            userid = MyContext.getSessionUser().getId();
        }
        return tractateService.getTractateById(id, userid);
    }

    @CrossOrigin
    @ApiOperation(value = "添加评论", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "tractateid", value = "短文id", required = true),
            @ApiImplicitParam(name = "content", value = "评论内容", required = true) })
    @PostMapping("/saveCriticism")
    public Result saveCriticism(@RequestParam("tractateid") Long tractateid, @RequestParam("content") String content,
            @ApiIgnore HttpSession session) {
        User user = (User) session.getAttribute(userAttr);
        Criticism criticism = new Criticism(null, user.getId(), tractateid, content, 0, null, null);
        return tractateService.saveCriticism(criticism);
    }

    @CrossOrigin
    @ApiOperation(value = "添加短文", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "title", value = "短文标题", required = true),
            @ApiImplicitParam(name = "ftitle", value = "短文副标题", required = true),
            @ApiImplicitParam(name = "level", value = "短文难度", required = true),
            @ApiImplicitParam(name = "content", value = "短文内容", required = true) })
    @PostMapping("/saveTractate")
    public Result saveTractate(@RequestParam("title") String title, @RequestParam("ftitle") String ftitle,
            @RequestParam("level") Integer level, @RequestParam("content") String content) {
        Tractate tractate = new Tractate(null, title, ftitle, level, content, null, null, false);
        log.info("saveTractate:" + tractate.toString());
        return tractateService.saveTractate(tractate);
    }

    @CrossOrigin
    @ApiOperation(value = "学生获取短文", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "pageNo", value = "当前页数", required = false),
            @ApiImplicitParam(name = "pageNum", value = "每页数量", required = false) })
    @GetMapping("/getTractates")
    public Result getTractates(@RequestParam(value = "pageNo", defaultValue = "1", required = false) Integer pageNo,
            @RequestParam(value = "pageNum", defaultValue = "10", required = false) Integer pageNum,
            @ApiIgnore HttpSession session) {
        User user = (User) session.getAttribute(userAttr);
        return tractateService.getTractates(user.getLevel(), pageNo, pageNum);
    }

    @CrossOrigin
    @ApiOperation(value = "管理员获取短文", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "pageNo", value = "当前页数", required = false),
            @ApiImplicitParam(name = "pageNum", value = "每页数量", required = false) })
    @GetMapping("/getTractatesAll")
    public Result getTractatesAll(@RequestParam(value = "pageNo", defaultValue = "1", required = false) Integer pageNo,
            @RequestParam(value = "pageNum", defaultValue = "10", required = false) Integer pageNum,
            @ApiIgnore HttpSession session) {
        return tractateService.getTractates(5, pageNo, pageNum);
    }
}
